package level

import (
	"github.com/gin-gonic/gin"
	"smartLearn/models"
	"smartLearn/modules/response"
	"strconv"
)

func GetLevelAll(c *gin.Context) {
	levelModel := models.SystemLevel{};
	levelList,err:=levelModel.GetAll()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var list = map[string] []models.SystemLevel{
		"list":levelList,
	}

	response.ShowData(c,list)
	return

}

func Add(c *gin.Context) {
	/*
	处理输入参数
	 */
	name:=c.Query("name")

	parentId,err:=strconv.Atoi(c.Query("parentId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	level,err:=strconv.Atoi(c.Query("level"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	systemLevel:=models.SystemLevel{}
	systemLevel.Name=name
	systemLevel.Parentid=parentId
	systemLevel.Level=level
	err=systemLevel.Add()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"添加阶段成功！")

}

func Update(c *gin.Context) {
	/*
		处理输入参数
	*/
	name:=c.Query("name")

	parentId,err:=strconv.Atoi(c.Query("parentId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	level,err:=strconv.Atoi(c.Query("level"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	id,err:=strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	systemLevel:=models.SystemLevel{}
	systemLevel.Id=id
	systemLevel.Name=name
	systemLevel.Parentid=parentId
	systemLevel.Level=level
	err=systemLevel.Update()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"更新阶段成功！")

}

func Delete(c *gin.Context) {

	levelModel := models.SystemLevel{};

	var err error
	levelModel.Id,err =strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	err=levelModel.Delete()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"删除阶段成功" )

}

